import { constantRoutes, asyncRoutes } from '@/router'

const state = {
  routes: constantRoutes
}

const mutations = {
  setRoutes(state, otherRoutes) {
    // 合并路由规则
    state.routes = [...constantRoutes, ...otherRoutes]
  }
}

const actions = {
  filterRoutes(context, menus) {
    const otherRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].name))
    context.commit('setRoutes', otherRoutes)
    return otherRoutes // 过滤后的动态路由返回出去用于调用该方法时
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
